package com.supermap.shiro.auth.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.user.LoginService;
import com.supermap.dubbo.api.user.entity.User;
import com.supermap.shiro.auth.entity.AppUser;
import com.supermap.shiro.auth.service.AppUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author xiaolu
 * @date 2018/7/2 上午10:03
 */
@Service
public class AppUserServiceImpl implements AppUserService {
    private static Logger logger = LoggerFactory.getLogger(AppUserService.class);
    @Reference
    private LoginService loginService;

    @Override
    public AppUser login(String userName, String password) {
        logger.debug("Get Dubbo LoginService:" + Objects.isNull(loginService));
        AppUser appUser = new AppUser(userName, password);
        JSONUtil result = loginService.login(userName, password);
        if (result != null && result.getCode() == 200) {
            User user = (User) result.getData();
            logger.debug("Get User From LoginService:" + Objects.isNull(user));
            appUser = new AppUser(user);
            appUser.setRealPassword(password);
        }
        return appUser;
    }

    @Override
    public JSONObject register(AppUser appUser) {
        JSONObject result = new JSONObject();
        result.put("status", "Method Can Not Available");
        result.put("code", 404);
        return result;
    }

}
